package org.smart4j.chapter2.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smart4j.chapter2.helper.DatabaseHelper;
import org.smart4j.chapter2.model.Customer;
import java.util.List;
import java.util.Map;

/**
 * Created by ly on 2018/7/18.
 * 提供客户数据服务
 */
public class CustomerService {
    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerService.class);

    /**
     * 获取客户列表
     */
    public List<Customer> getCustomerList(){
        //TODO
        String sql = "SELECT * FROM customer";
        return DatabaseHelper.queryEntityList(Customer.class, sql);
     /*   try {
            String sql = "select * from customer";
            conn = DatabaseHelper.getConnection();
            PreparedStatement stmt =  conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()){
                Customer customer = new Customer();
                customer.setId(rs.getLong("id"));
                customer.setName(rs.getString("name"));
                customer.setContact(rs.getString("contact"));
                customer.setTelephone(rs.getString("telephone"));
                customer.setEmail(rs.getString("email"));
                customer.setRemark(rs.getString("remark"));
                customers.add(customer);
            }

        } catch (SQLException e) {
            LOGGER.error("execute sql failure",e);
        }finally {
            DatabaseHelper.closeConnection(conn);
        }
        return customers;*/
    }

    /**
     * 获取客户
     */
    public Customer getCustomer(long id){
        //todo
        return null;
    }

    /**
     * 创建客户
     */
    public boolean createCustomer(Map<String,Object> fieldMap){
        //todo
        return DatabaseHelper.insertEntity(Customer.class,fieldMap);
    }
    /**
     * 更新客户
     */
    public boolean updateCustomer(long id,Map<String,Object> fieldMap){
        //todo
        return DatabaseHelper.updateEntity(Customer.class,id,fieldMap);
    }
    /**
     * 删除客户
     */
    public boolean deleteCustomer(long id){
        //todo
        return DatabaseHelper.deleteEntity(Customer.class,id);
    }

}
